package com.svo.weitu.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.svo.weitu.R;
import com.svo.weitu.util.UiUtil;
import com.svo.weitu.view.MeshView;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.io.FileNotFoundException;
import java.io.IOException;

public class NieActivity extends Activity{
	private MeshView meshView;
	Bitmap initBitmap = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("好友戳一戳");
		setContentView(R.layout.nie);
		Intent intent = getIntent();
		if (intent != null) {
			if (intent.getStringExtra("from").equals("pick")) {
				try {
					initBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), (Uri) intent.getParcelableExtra("uri"));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if (intent.getStringExtra("from").equals("PicBrowser")) {
				initBitmap = BitmapFactory.decodeFile(intent.getStringExtra("picPath"));
			}else{
				initBitmap = (Bitmap) intent.getParcelableExtra("bitmap");
			}
		}
		meshView = new MeshView(this);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		meshView.setLayoutParams(params);
		meshView.setScaleType(ScaleType.FIT_CENTER);
		initBitmap = zoomBitmap(initBitmap);
		meshView.setBitmap(initBitmap);
		RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.par);
		linearLayout.addView(meshView);
	}
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}
	private int desW;
	private int desH;
    /**
	 * 缩放图片
	 * @param bitmap
	 * @param w 目标宽度
	 * @param h 目标高度
	 * @return bitmap
	 */
    public Bitmap zoomBitmap(Bitmap bitmap){   
    	DisplayMetrics metrics = getResources().getDisplayMetrics();
    	int w = metrics.widthPixels;
    	int h = (int) (metrics.heightPixels - metrics.density*(50+48));
        int width = bitmap.getWidth();   
        int height = bitmap.getHeight();   
        float scaleWidht = ((float)w / width);   
        float scaleHeight = ((float)h / height);   
        float scaleFactor = scaleHeight;
        if (scaleWidht < scaleHeight) {
        	scaleFactor = scaleWidht; 
		}
        desH = (int) (scaleFactor*height);
        desW = (int) (scaleFactor*width);
        Bitmap newbmp =  Bitmap.createScaledBitmap(bitmap, desW, desH, true);
        return newbmp;   
    } 
	public void click(View view) {
		if (R.id.recover == view.getId()) {
			if (initBitmap != null) {
				meshView.setBitmap(initBitmap);
			}
		} else {
			new ShareAction(NieActivity.this)
		    .withMedia(new UMImage(this, shot()))
			.setDisplayList(SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN_FAVORITE)
		    .setCallback(new UMShareListener() {

				@Override
				public void onStart(SHARE_MEDIA arg0) {
					UiUtil.toast(getApplicationContext(),"分享开始");
				}

				@Override
				public void onResult(SHARE_MEDIA arg0) {
					UiUtil.toast(getApplicationContext(),"分享成功");
				}

				@Override
				public void onError(SHARE_MEDIA arg0, Throwable arg1) {
					UiUtil.toast(getApplicationContext(),"分享出错");
				}

				@Override
				public void onCancel(SHARE_MEDIA arg0) {
					UiUtil.toast(getApplicationContext(),"分享取消");
				}
			})
		    .open();
		}
	}
	public Bitmap shot() {
		View view = meshView;
		view.layout(0, 0, desW, desH);
		view.destroyDrawingCache();
		view.setDrawingCacheEnabled(true);// 允许当前窗口保存缓存信息，这样getDrawingCache()方法才会返回一个Bitmap
		Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache());
		meshView.setBitmap(bmp);
		return bmp;
	}
	@Override 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    super.onActivityResult(requestCode, resultCode, data);
	}
}
